using UnityEngine;
using System.Collections;

public class TileMap{
	
	
	private Texture2D baseTileMap;
	public int tileSize;
	public Texture2D[] tiles;
	public int width;
	public int counter = 0;
    public bool done = false;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public TileMap(Texture2D baseTiles,int tileSize)
    {
        this.tileSize = tileSize;
        this.baseTileMap = baseTiles;
        int targetWidth = baseTiles.width / tileSize;
        this.width = targetWidth;
        tiles = new Texture2D[targetWidth * targetWidth];

        //used to hold the color data for each tile
        Color[] tileColor;
        //need to use a seperate variable to track where in the tiles[] array to
        //place tile info.  otherwise it gets reversed due to row counting down
        //instead of up
        int targetIndex = 0;

        //for (int row = 0; row < targetWidth; row++)
        for(int row = targetWidth - 1; row >=0; row--) //I want 0 in the upper left.  I just do.  Don't judge me.
        {
            for (int col = 0; col < targetWidth; col++)
            {
                //find the x,y of the source tile map
                int sourceX = col * tileSize;
                int sourceY = row * tileSize;
                //grab that chunk of color data
                tileColor = baseTiles.GetPixels(sourceX, sourceY, tileSize, tileSize);
                //create the tile and assign the color data
                tiles[targetIndex] = new Texture2D(tileSize, tileSize, TextureFormat.ARGB32, false);
                tiles[targetIndex].SetPixels(tileColor);
                //***ALWAYS*** remember to call Apply() when altering color data
                tiles[targetIndex].Apply();
                targetIndex++;
                counter = targetIndex;
            }
        }//end row for
        done = true;
    }//end TileMap()
}
